#include "RenderManager.h"

RenderManager* RenderManager::renderMgr = NULL;


RenderManager::RenderManager(void)
{
	setup();
}
RenderManager::~RenderManager()
{
	cleanup();
}

RenderManager* RenderManager::getSingleton()
{
	if(renderMgr == NULL)
	{
		renderMgr = new RenderManager();
	}
	return renderMgr;
}

//===

bool RenderManager::setup(void)
{
	initialise();
	setupResources();
	createWindow();
	createFrameListener();

	return true;
}

void RenderManager::initialise(void)
{
	m_Filtering = Ogre::TFO_BILINEAR;				// Initialise texture filtering
	m_iAniso = 1;									// Level for anisotrophic filtering
	m_bStatsOn = true;

	// Create ogre root. Initialising root requires to a plugin and resources file.
	// This is left empty so that it can we can specify uniquely the resources and
	// plugins we want to run by Ogre.
	m_pRoot = new Ogre::Root("","");

	// Debug format mode, this is initialise the default DirectX render system.
	// Remove "_d" for release format
	m_pRoot->loadPlugin("RenderSystem_Direct3D9_d");
	Ogre::RenderSystemList* renderSystems = NULL;
	Ogre::RenderSystemList::iterator r_it;

	// Step through available renderers to use in the setRenderSystem.
	renderSystems = m_pRoot->getAvailableRenderers();
	r_it = renderSystems->begin();
	m_pRoot->setRenderSystem(*r_it);
	m_pRoot->initialise(false);

	// Load the common plugins
	m_pRoot->loadPlugin("Plugin_CgProgramManager_d");
	m_pRoot->loadPlugin("Plugin_OctreeSceneManager_d");
}

void RenderManager::setupResources(void)
{
	// Load resource locations. This is done here manually.
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
		"../resource", "FileSystem", "General");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
		"../resource/mesh", "FileSystem", "Mesh");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
		"../resource/gui.zip", "Zip", "GUI");
#if defined(WIN32)
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
		"c:\\windows\\fonts", "FileSystem", "GUI");
#endif

	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("General");
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Mesh");
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("GUI");

}

void RenderManager::createWindow(void)
{
	Ogre::NameValuePairList opts;
	opts["resolution"] = "1024x768";
	opts["fullscreen"] = "false";
	opts["vsync"] = "false";

	// Create window
	m_pRenderWindow = m_pRoot->createRenderWindow("AGT RPG",1024,768,false,&opts);

	// Get the handle to the window
	m_pRenderWindow->getCustomAttribute("WINDOW", &m_hWnd);

	// Setup icon for the window
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
	HWND handle;
	m_pRenderWindow->getCustomAttribute("WINDOW",(void*)&handle);
	LONG iconID = (LONG)LoadIcon(GetModuleHandle(0),MAKEINTRESOURCE(IDI_APPICON)  );
	SetClassLong(handle, GCL_HICON, iconID);
#endif

}

void RenderManager::createFrameListener(void)
{
	m_ShutDownRequested = false;
	m_pRoot->addFrameListener(this);
}

void RenderManager::startRendering(void)
{
	m_pRoot->startRendering();
}

bool RenderManager::frameStarted(const Ogre::FrameEvent& evt)
{
	if(m_pRenderWindow->isClosed())
		return false;

	return true;
}

bool RenderManager::frameEnded(const Ogre::FrameEvent& evt)
{
	if(m_ShutDownRequested)
		return false;
	return true;
}

void RenderManager::cleanup(void)
{
}